import conf from '../conf'


const io = {

  configUrls: function () {
    //报告
    this.findTrim = conf.baseApiPath + '/terminal/api/student/findTrim'
    this.reportStudentList = conf.baseApiPath + '/terminal/api/student/reportStudentList'
    this.reportInfo = conf.baseApiPath + '/terminal/api/student/reportInfo'
    this.reportScore = conf.baseApiPath + '/terminal/api/student/reportScore'
    this.reportQues = conf.baseApiPath + '/terminal/api/student/reportQues'
    this.reportHard = conf.baseApiPath + '/terminal/api/student/reportHard'
    this.reportKnow = conf.baseApiPath + '/terminal/api/student/reportKnow'
    this.reportKnowI = conf.baseApiPath + '/terminal/api/student/reportKnowI'
    //惠州报告
    this.reportListOfStudent = conf.baseApiPath + '/terminal/api/student/reportListOfStudent'
    this.reportPass = conf.baseApiPath + '/terminal/api/student/reportPass'

    this.findCustomerBindStudent = conf.baseApiPath + '/terminal/api/student/findCustomerBindStudent'

  },
  getHeaders: function () {
    const accessToken = localStorage.getItem('accessToken') || '';
    return {
      accessToken
    }

  },
  get: function (url, data, success, fail) {
    data.accessToken = this.getHeaders().accessToken
    $.ajax({
      url: url,
      type: 'GET',
      data: data,
      dataType: 'json',
      cache: true,
      timeout: 30000,
      success: function (data) {
        // if (!checkResult(data)) {
        //   return;
        // }
        if (success) {
          success(data);
        }
      },
      error: function (xhr, status, error) {
        if (fail) {
          fail(xhr, status, error);
        } else {
          //alert("服务端出错");
        }
      }
    });
  },
  post: function (url, data, success, fail) {
    data.accessToken = this.getHeaders().accessToken
    $.ajax({
      url: url,
      type: 'POST',
      data: data,
      dataType: 'json',
      timeout: 30000,
      success: function (data) {
        // if (!checkResult(data)) {
        //   return;
        // }
        if (success) {
          success(data);
        }
        if(!data.success){
          alert(data.desc)
        }
      },
      error: function (xhr, status, error) {
        if (fail) {
          fail(xhr, status, error);
        } else {
          //alert("服务端出错");
        }
      }
    });
  },
  syncPost: function (url, data, success, fail) {
    data.accessToken = this.getHeaders().accessToken
    $.ajax({
      url: url,
      type: 'POST',
      data: data,
      dataType: 'json',
      timeout: 30000,
      async: false,
      success: function (data) {
        // checkResult(data)
        if (success) {
          success(data);
        }
      },
      error: function (xhr, status, error) {
        if (fail) {
          fail(xhr, status, error);
        } else {
          //alert("服务端出错");
        }
      }
    });
  },
  postPlayload: function (url, data, success, fail) {
    data.accessToken = this.getHeaders().accessToken
    $.ajax({
      url: url,
      type: 'POST',
      contentType: 'application/json; charset=UTF-8',
      data: JSON.stringify(data),
      dataType: 'json',
      processData: false,
      timeout: 30000,
      success: function (data) {
        // checkResult(data)
        if (success) {
          success(data);
        }
      },
      error: function (xhr, status, error) {
        if (fail) {
          fail(xhr, status, error);
        } else {
          //alert("服务端出错");
        }
      }
    });
  },
  getCity: function () {
    var city = localStorage.getItem('city');
    var currentCity = sessionStorage.getItem('city');
    return city ? city : (currentCity ? currentCity : '广州')
  },
  ajax: function (method, url, data, cb) {
    if (ajaxTimes > 5) { return }
    data.accessToken = localStorage.getItem('accessToken');
    $.ajax({
      method: method,
      url: this.host + url,
      data: data,
      dataType: 'json',
      success: function (ret) {
        if (!ret.success && ret.desc && ret.desc.indexOf('oken') != -1) {

          getAccessToken(localStorage.getItem('openId'), function () {
            ajaxTimes++;
            io.ajax(method, url, data, cb)
            ajaxTimes = 0;
          })
        } else {
          ajaxTimes = 0;
          cb && cb(ret)
        }

      },
      error: function (error) {
        console.error(error);
      }
    });
  },
  ajaxWithoutToken: function (method, url, data, cb) {
    $.ajax({
      method: method,
      url: this.host + url,
      data: data,
      dataType: 'json',
      success: cb,
      error: function (error) {
        console.error(error);
      }
    });
  }
};

io.configUrls()

export default io;
